package com.ntschy.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ntschy.common.core.entity.dataobject.SysUser;
import com.ntschy.common.core.entity.enums.SysCategoryEnum;
import com.ntschy.sys.mapper.SysUserMapper;
import com.ntschy.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ezreal_geng
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper userMapper;

    // * ================================================基础增删改查BEGIN============================================= *
    /**
     * 分页查询用户
     * 当前查询条件：根据用户名｜用户类别查询 时间倒序
     * @param user 用户对象
     * @return 用户列表
     */
    @Override
//    @ListDataScope(mainAlias = "u", mainField = "CreateUserID")
    public List<SysUser> pageUser(SysUser user) {
        return userMapper.pageUser(user);
    }

    /**
     * 新增用户
     * @param user 用户对象
     * @return 新增结果
     */
    @Override
    public int addUser(SysUser user) {
        user.setId(IdUtil.fastUUID());
        // TODO 设置初始密码
        user.setPassword("$2a$10$TFno2mL3BWVuvQuYUa.AO.KS.h1.v9A2p2PulYZL2f.MNGR33ZI2y");
        return userMapper.insert(user);
    }

    /**
     * 更新用户
     * @param user 用户对象
     * @return 更新结果
     */
    @Override
    public int updateUser(SysUser user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(String userId) {
        return userMapper.deleteById(userId);
    }

    // * =================================================基础增删改查END============================================== *


    /**
     * 根据用户名获取用户详细信息
     * 包括角色｜单位｜部门 信息
     * 当前用到此方法场景: 登陆验证用户名密码创建token时
     * @param username 用户名
     * @return 用户详细信息
     */
    @Override
    public SysUser fetchUserDetailByUserName(String username) {
        return userMapper.fetchUserDetailByUserName(username);
    }

    /**
     * 获取部门下全部用户ID
     * @param deptId 部门ID
     * @return 用户id集合
     */
    @Override
    public List<String> listUserIdByDept(String deptId) {
        List<SysUser> userList = userMapper.selectList(new QueryWrapper<SysUser>().lambda().eq(SysUser::getDept, deptId).select(SysUser::getId));
        return userList.stream().map(SysUser::getId).collect(Collectors.toList());
    }

    /**
     * 获取单位下全部用户ID
     * @param companyId 单位ID
     * @return 用户id集合
     */
    @Override
    public List<String> listUserIdByCompany(String companyId) {
        List<SysUser> userList = userMapper.selectList(new QueryWrapper<SysUser>().lambda().eq(SysUser::getCompanyId, companyId).select(SysUser::getId));
        return userList.stream().map(SysUser::getId).collect(Collectors.toList());
    }

    @DS("mater")
    public void addCategoryMaster(SysUser user){
        user.setCategory(SysCategoryEnum.XJDW);
        userMapper.updateById(user);
    }

    @DS("salve")
    public void addCategorySalver(SysUser user){
        user.setCategory(SysCategoryEnum.YHDW);
        userMapper.updateById(user);
    }
}
